# -*- coding:utf-8 -*-

__author__='zhaoxp'


'''
class Foo(Bar):
    pass
Python做了如下的操作：

Foo中有__metaclass__这个属性吗？如果是，Python会在内存中通过__metaclass__创建一个名字为Foo的类对象
（我说的是类对象，请紧跟我的思路）。如果Python没有找到__metaclass__，它会继续在Bar（父类）中寻找
__metaclass__属性，并尝试做和前面同样的操作。如果Python在任何父类中都找不到__metaclass__，它就会在
模块层次中去寻找__metaclass__，并尝试做同样的操作。如果还是找不到__metaclass__,Python就会用内置的
type来创建这个类对象。
'''


class Meta(type):
    def __new__(meta,name,bases,class_dict):
        print((meta,name,bases,class_dict))
        return type.__new__(meta,name,bases,class_dict)

'''
# for python 3.*
class MyClass(object,metaclass=Meta):
    stuff = 123

    def foo(self):
        pass
'''

class MyClassInPython2(object):
    __metaclass__ = Meta
    pass


class ValidatePolygon(type):
    def __new__(meta,name,bases,class_dict):
        if bases != (object,):
            if class_dict['sides'] < 3:
                raise ValueError('Polygon need 3+ sides')
        return type.__new__(meta,name,bases,class_dict)
        #return super(ValidatePolygon,meta).__new__(name,bases,class_dict)


class Polygon(object):
    __metaclass__=ValidatePolygon

    @classmethod
    def interior_angles(cls):
        return (cls.sides - 2)*180


class Triangle(Polygon):
    sides=3


print('before class Line')
class Line(Polygon):
    print('before sides')
    sides = 1
    print('before other_args')
    other_args = 'test'
    print('after other_args')
    print('after sides')
print('after class Line')

if __name__=='__main__':
    print('Validate subclasses with metaclasses')